package shop_cart

import (
	"context"
	goods_pb "gitee.com/fangy1111/shop/goodsPb"
	"gitee.com/fangy1111/shop/inventory"
	"gitee.com/fangy1111/shop/order"
	"github.com/gin-gonic/gin"
	"net/http"
	"order_bff/global"
	"order_bff/model"
	"strconv"
)

// 购物车列表
func List(c *gin.Context) {
	userId, _ := c.Get("uid")

	list, err := global.OrderSrvConn.CartItemList(context.Background(), &order.UserInfo{
		Id: userId.(int32),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "查询购物车列表失败",
			"data": nil,
		})
		return
	}

	var goodsIds []int32
	for _, v := range list.Data {
		goodsIds = append(goodsIds, v.GoodsId)
	}

	goods, err := global.GoodsSrvConn.BatchGetGoods(context.Background(), &goods_pb.BatchGoodsIdInfo{Id: goodsIds})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "批量查询商品信息失败",
			"data": nil,
		})
		return
	}

	var shopCart []*model.ShopCart

	for _, item := range list.Data {
		for _, v := range goods.Data {
			if item.GoodsId == v.Id {
				shopCart = append(shopCart, &model.ShopCart{
					ID:         item.Id,
					GoodsId:    item.GoodsId,
					GoodsName:  v.Name,
					GoodsImage: v.GoodsFrontImage,
					GoodsPrice: v.ShopPrice,
					Nums:       item.Nums,
					Checked:    item.Checked,
				})
			}
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": shopCart,
	})
}

// 添加购物车
func Create(c *gin.Context) {

	goodsId := c.PostForm("goods_id")
	goodsIdInt, _ := strconv.Atoi(goodsId)
	num := c.PostForm("num")
	numInt, _ := strconv.Atoi(num)

	if len(goodsId) == 0 || len(num) == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "参数不全",
			"data": nil,
		})
		return
	}

	//添加购物车前，检查一下商品是否存在
	_, err := global.GoodsSrvConn.GetGoodsDetail(context.Background(), &goods_pb.GoodInfoRequest{Id: int32(goodsIdInt)})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}

	//判断库存是否充足
	detail, err := global.InventoryConn.GetStockDetail(context.Background(), &inventory.GoodsInvInfo{
		GoodsId: int32(goodsIdInt),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "库存信息查询失败",
			"data": nil,
		})
		return
	}

	if detail.Stock < int32(numInt) {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "库存不足",
			"data": nil,
		})
		return
	}

	//添加购物车
	userId, _ := c.Get("uid")
	rep, err := global.OrderSrvConn.CreateCartItem(context.Background(), &order.CartItemRequest{
		UserId:  userId.(int32),
		GoodsId: int32(goodsIdInt),
		Nums:    int32(numInt),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "添加购物车失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "添加购物车成功",
		"data": map[string]interface{}{
			"id":       rep.Id,
			"goods_id": rep.GoodsId,
			"nums":     rep.Nums,
			"checked":  rep.Checked,
		},
	})
}

// 更新购物车
func Update(c *gin.Context) {
	num := c.PostForm("num")
	numInt, _ := strconv.Atoi(num)

	id := c.PostForm("id")
	idInt, _ := strconv.Atoi(id)

	//更新购物车
	_, err := global.OrderSrvConn.UpdateCartItem(context.Background(), &order.CartItemRequest{
		Id:      int32(idInt),
		Nums:    int32(numInt),
		Checked: true,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "更新购物车失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "更新购物车成功",
		"data": nil,
	})

}

// 删除购物车
func Delete(c *gin.Context) {

	id := c.PostForm("id")
	idInt, _ := strconv.Atoi(id)

	if len(id) == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "id不能为空",
			"data": nil,
		})
		return
	}

	_, err := global.OrderSrvConn.DeleteCartItem(context.Background(), &order.CartItemRequest{
		Id: int32(idInt),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  "删除购物车失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "删除购物车成功",
		"data": nil,
	})
}
